package com.zj.osm.view.map.offline

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.location.Location
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.FileUtils.listFilesInDir
import com.example.location_library.china_location.ChinaLocation.Companion.getInstance
import com.example.location_library.china_location.ChinaLocationNotifyChange
import com.example.location_library.china_location.ChineLocationBean
import com.example.location_library.china_location.LocationType
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import com.zj.osm.R
import com.zj.osm.view.BaseActivity
import com.zj.utils.utils.utilcode.SPUtils
import com.zj.utils.utils.utilcode.ToastUtils
import kotlinx.android.synthetic.main.activity_offline_map.*
import java.io.File

class OfflineMapActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_offline_map)
        initView()
    }

    override fun onResume() {
        super.onResume()
        if (SPUtils.getInstance().getString("DEFAULT_MAP_FLAG") == "ONLINE_MAP") {
            offIvOnline.visibility = View.VISIBLE
        } else {
            offIvOnline.visibility = View.INVISIBLE
        }
        offRv.adapter = OfflineMapAdapter(this, R.layout.adapter_offline, getFile())
    }

    private fun getFile(): List<File> {
        return listFilesInDir(Environment.getExternalStorageDirectory().path + "/osmdroid").filter {
            it.name.endsWith(".zip")
                    || it.name.endsWith(".sqlite")
                    || it.name.endsWith(".mbtiles")
                    || it.name.endsWith(".gemf")
        }
    }

    private fun initView() {
        //定位自己
        offTitleBar.setTitle(getString(R.string.view_map))
        offTitleBar.setBackImageVisiable(true)
        //offTitleBar.setRightText("创建")

        offRv.layoutManager = LinearLayoutManager(this)
        offLlOnline.setOnClickListener {
            SPUtils.getInstance().put("DEFAULT_MAP_FLAG", "ONLINE_MAP")
            ToastUtils.showShort("切换为在线地图,请注意流量消耗")
            finish()
            //startActivity(Intent(this, MapActivity::class.java))
        }
        offTitleBar.setRightTextOnClickListener {
            startActivity(Intent(this, DownOfflineMapActivity::class.java))
        }
    }



}

class OfflineMapAdapter(
        context: Context,
        layoutId: Int,
        rows: List<File>
) : CommonAdapter<File>(context, layoutId, rows) {

    override fun convert(holder: ViewHolder, t: File, position: Int) {
        val offAdLlItem = holder.getView<CardView>(R.id.offAdLlItem)
        val offAdTvName = holder.getView<TextView>(R.id.offAdTvName)
        val offAdIvOnline = holder.getView<ImageView>(R.id.offAdIvOnline)
        val mapFlag = SPUtils.getInstance().getString("DEFAULT_MAP_FLAG")
        if (mapFlag != "ONLINE_MAP" && mapFlag == t.name) {
            offAdIvOnline.visibility = View.VISIBLE
        }
        offAdTvName.text = t.name
        offAdLlItem.setOnClickListener {
            if (t.name.endsWith(".zip")
                    || t.name.endsWith(".sqlite")
                    || t.name.endsWith(".mbtiles")
                    || t.name.endsWith(".gemf")) {
                SPUtils.getInstance().put("DEFAULT_MAP_FLAG", t.name)
                //this.mContext.startActivity(Intent(this.mContext as Activity, MapActivity::class.java))
                (this.mContext as Activity).finish()
                ToastUtils.showShort("成功选择离线包")
            } else {
                ToastUtils.showShort("请选择有效地图包")
            }
        }
    }

}
